package com.lxk.concurrency.example.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description 
 *
 * @author 堆堆堆
 * @date 2018年5月25日 上午11:32:28
 */
public class LockExample6 {

	public static void main(String[] args) {
		ReentrantLock reentrantLock = new ReentrantLock();
		Condition condition = reentrantLock.newCondition();

		new Thread(() -> {
			try {
				reentrantLock.lock();
				System.out.println("wait signal"); // 1

				// 相当于 wait()，线程休眠，释放锁
				condition.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("get signal"); // 4
			reentrantLock.unlock();
		}).start();

		new Thread(() -> {
			reentrantLock.lock();
			System.out.println("get lock"); // 2
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			// 相当于 notifyAll()，唤醒所有线程
			condition.signalAll();
			System.out.println("send signal ~ "); // 3
			reentrantLock.unlock();
		}).start();
	}
}
